package pool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: ycy
 * @CreateDate: 2020/5/28 13:53
 */
public class NewObjectTest2 {


  static final Logger logger = LoggerFactory.getLogger(NewObjectTest2.class);

  /**
   * 为每个线程 创建一个indexDoc 缓存
   *
   * @param args
   */
  public static void main(String[] args) {

    ExecutorService executorService = Executors.newFixedThreadPool(100);

//    new Thread(() -> {
//      while (true) {
//        logger.info("indexDoc count {}!!!!!!!!!!!!!!!!!!!!!!!!! ", IndexDoc.count.get());
//        try {
//          Thread.sleep(30000);
//        } catch (InterruptedException e) {
//          e.printStackTrace();
//        }
//      }
//    }).start();

    while (true) {
      try {
        //600次/min
        //1500
        Thread.sleep(40);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      Thread thread1 = new Thread(() -> {
        //初始化 为请求线程初始化一个indexDoc缓存
        IndexDocThreadLocal.init();
        //模拟用户请求
        new NewObjectTest2().request();
      });
      executorService.submit(thread1);
    }
  }

  //每次请求创建 3w 个index
  public void request() {
    long l = System.currentTimeMillis();
    try {
      for (int i = 0; i < 30001; i++) {
        IndexDoc index = IndexDocThreadLocal.getIndex();
        index.word = "  数目";
      }
    } catch (Exception e) {
      logger.error("fail {}", e.getMessage(), e);
    } finally {
      IndexDocThreadLocal.resizeIndex();
    }
    logger.info("cost:{} , indexcount {}", (System.currentTimeMillis() - l), IndexDocThreadLocal.getIndexCacheSize());
  }

}
